home *** CD-ROM | disk | FTP | other *** search
- /*
- * A simple program for messing around with the new Speech Manager.
- * By Alex Kourakos
- * kourakos@ncsc.org
- */
-
- #include <Speech.h>
- #include <Errors.h>
- #include <GestaltEqu.h>
- #include <FixMath.h>
- #include <pascal.h>
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
-
- /*
- * Globals.
- */
-
- SpeechChannel gSpeechChannel = nil; // The current speech channel.
- VoiceDescription gVoiceDescription; // A record containing useful (?) information.
- short gCountVoices, // The number of voices available.
- gCurrentVoiceIndex; // The number of the current voice.
-
- char gSayThis[255]; // The string that we want to say.
-
- Boolean gDone = false; // Are we through with this fine program?
-
-
- /*
- * Function prototypes.
- */
-
- void main(void);
- void TestForSpeechManager(void);
- void Initialize(void);
- void ReadLine(void);
- void LoadVoice(short);
- void HandleError(OSErr);
- void PrintHelp(void);
-
-
- /*
- * Main.
- */
-
- void main(void) {
- OSErr err;
-
- TestForSpeechManager();
-
- Initialize();
-
- while( !gDone )
- ReadLine();
-
- /*
- * Loop until quiet so that we don't cut the voice off.
- */
-
- while( SpeechBusy() )
- ;
-
- /*
- * Let's be polite and kill the speech channel as we leave.
- */
-
- if( gSpeechChannel != nil ) {
- err = DisposeSpeechChannel(gSpeechChannel);
- if( err != noErr) HandleError(err);
- }
-
- ExitToShell();
- }
-
-
- /*
- * Use Gestalt to see if the Speech Manager is around.
- */
-
- void TestForSpeechManager(void) {
- long result;
- OSErr err;
-
- err = Gestalt(gestaltSpeechAttr,&result);
-
- if( err != noErr )
- HandleError(err);
-
- if( !(result & (1L << gestaltSpeechMgrPresent)) ) {
- fprintf(stderr,"Sorry, you do not have the Speech Manager installed.\n");
- exit(EXIT_FAILURE);
- }
- }
-
-
- /*
- * Initialize the voice, etc.
- */
-
- void Initialize(void) {
- OSErr err;
-
- /*
- * Just in case somebody types "!!" right away.
- */
-
- sprintf(gSayThis,"The quick brown fox jumps over the lazy dog.\n");
-
- /*
- * Find out how many voices we have.
- */
-
- err = CountVoices(&gCountVoices);
- if( err != noErr) HandleError(err);
-
- /*
- * Load number 1.
- */
-
- LoadVoice(1);
-
- /*
- * Print the list of commands.
- */
-
- printf("Speech Manager demo - by Alexander Kourakos - kourakos@ncsc.org\n");
- PrintHelp();
- }
-
-
- /*
- * Read in a line. If it is a command, do it, otherwise, say it.
- */
-
- void ReadLine(void) {
- char string[255];
- OSErr err;
- short newVoiceIndex;
- Fixed aFixed;
- long aLong;
-
- printf("> ");
- gets(string);
-
- /*
- * Process a voice command.
- */
-
- if( string[0] == 'v' ) {
- if( string[1] == '?' ) {
- printf("Current voice: %d (%s, ",gCurrentVoiceIndex,(char *)gVoiceDescription.name);
- switch( gVoiceDescription.gender ) {
- case kNeuter:
- printf("neuter");
- break;
- case kMale:
- printf("male");
- break;
- case kFemale:
- printf("female");
- break;
- }
- printf(", age %hd)\n\n",gVoiceDescription.age);
- return;
- }
-
- if( string[1] == ' ' ) {
- sscanf(string,"%*s %d",&newVoiceIndex);
- LoadVoice(newVoiceIndex);
- return;
- }
- }
-
- /*
- * Process a rate command.
- */
-
- if( string[0] == 'r' ) {
- if( string[1] == '?' ) {
- err = GetSpeechRate(gSpeechChannel,&aFixed);
- if( err != noErr) HandleError(err);
-
- printf("Current speech rate: %ld\n\n",Fix2Long(aFixed));
- return;
- }
-
- if( string[1] == ' ' ) {
- sscanf(string + 2," %ld",&aLong);
-
- err = SetSpeechRate(gSpeechChannel,Long2Fix(aLong));
- if( err != noErr) HandleError(err);
- return;
- }
- }
-
- /*
- * Process a pitch command.
- */
-
- if( string[0] == 'p' ) {
- if( string[1] == '?' ) {
- err = GetSpeechPitch(gSpeechChannel,&aFixed);
- if( err != noErr) HandleError(err);
-
- printf("Current pitch: %ld\n\n",Fix2Long(aFixed));
- return;
- }
-
- if( string[1] == ' ' ) {
- sscanf(string + 2," %ld",&aLong);
-
- err = SetSpeechPitch(gSpeechChannel,Long2Fix(aLong));
- if( err != noErr) HandleError(err);
- return;
- }
- }
-
- /*
- * Do they want help?
- */
-
- if( !strcmp(string,"??") ) {
- PrintHelp();
- return;
- }
-
- /*
- * Do they want to quit?
- */
-
- if( !strcmp(string,"quit") ) {
- gDone = true;
- return;
- }
-
- /*
- * If it isn't "!!", then copy the string into the string to speak.
- */
-
- if( strcmp(string,"!!") ) {
- strcpy(gSayThis,string);
- }
-
- /*
- * Stop speech on this channgel.
- * (Is this necessary? Couldn't hurt...)
- */
-
- err = StopSpeech(gSpeechChannel);
- if( err != noErr) HandleError(err);
-
- /*
- * Speak the text.
- */
-
- err = SpeakText(gSpeechChannel,(Ptr)gSayThis,strlen(gSayThis));
- if( err != noErr) HandleError(err);
- }
-
-
- /*
- * Loads in a voice with the given index, and sets the global variables appropriately.
- */
-
- void LoadVoice(short index) {
- OSErr err;
- VoiceSpec voiceSpec;
-
- err = GetIndVoice(index,&voiceSpec);
- if( err != noErr) HandleError(err);
-
- /*
- * Get the description and stash it in a global variable so other
- * functions can get the info.
- */
-
- err = GetVoiceDescription(&voiceSpec,&gVoiceDescription,sizeof(VoiceDescription));
- if( err != noErr) HandleError(err);
-
- /*
- * So we can print with ANSI routines...
- */
-
- (void)PtoCstr((void *)gVoiceDescription.name);
-
- /*
- * If there was an old channel, trash it.
- */
-
- if( gSpeechChannel != nil ) {
- err = DisposeSpeechChannel(gSpeechChannel);
- if( err != noErr) HandleError(err);
- }
-
- /*
- * Create a new channel using this voice specification. I don't know if
- * this is necessary, maybe I can just assign a new voice spec to the
- * channel somehow.
- */
-
- err = NewSpeechChannel(&voiceSpec,&gSpeechChannel);
- if( err != noErr) HandleError(err);
-
- gCurrentVoiceIndex = index;
- }
-
-
- /*
- * Prints the commands.
- */
-
- void PrintHelp(void) {
- printf("Commands are:\n");
- printf("quit - to quit\n");
- printf("v? - display current voice info\n");
- printf("v <n> - set current voice to <n> [1-%hd]\n",gCountVoices);
- printf("r? - display current speech rate\n");
- printf("r <n> - set speech rate to <n> [0 - 500]\n");
- printf("p? - display current pitch\n");
- printf("p <n> - set pitch to <n> [0 - 127]\n");
- printf("!! - repeat last phrase\n");
- printf("?? - this list of commands\n");
- printf("\n");
- }
-
-
- /*
- * Oops, something went wrong.
- */
-
- void HandleError(OSErr err) {
- fprintf(stderr,"\a\n\nSorry, an error occurred: ");
-
- switch( err ) {
-
- /*
- * The Speech Manager errors.
- */
-
- case noSynthFound:
- fprintf(stderr,"No synth found.\n");
- break;
- case synthOpenFailed:
- fprintf(stderr,"Synth open failed.\n");
- break;
- case synthNotReady:
- fprintf(stderr,"Synth not ready.\n");
- break;
- case bufTooSmall:
- fprintf(stderr,"Buffer too small.\n");
- break;
- case voiceNotFound:
- fprintf(stderr,"Voice not found.\n");
- break;
- case incompatibleVoice:
- fprintf(stderr,"Incompatible voice.\n");
- break;
- case badDictFormat:
- fprintf(stderr,"Bad dictionary format.\n");
- break;
- case badInputText:
- fprintf(stderr,"Bad input text.\n");
- break;
-
- /*
- * Other errors that may occur.
- */
-
- case gestaltUnknownErr:
- fprintf(stderr,"Gestalt doesn't have a clue.\n");
- break;
- case gestaltUndefSelectorErr:
- fprintf(stderr,"Unknown Gestalt selector.\n");
- break;
- case paramErr:
- fprintf(stderr,"Parameter error.\n");
- break;
-
- /*
- * Default: print the number and quit.
- */
-
- default:
- fprintf(stderr,"Error %d.\n",(int)err);
- }
- exit(EXIT_FAILURE);
- }
-
-